﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Secams.model;
using Secams.controller;
using Secams.utils;

namespace Secams
{
    public partial class FormParticipant : Form
    {
        protected ParticipantInfo _participantInfo = new ParticipantInfo();

        public FormParticipant(ParticipantInfo participantInfo)
        {
            _participantInfo = participantInfo;

            this.Font = AppContext.getDefaultFont(this.Font);
            InitializeComponent();

            SettingGroup educLevel = AppContext.getInstance().settings.getGroup("EducationLevel");
            foreach (KeyValuePair<string, string> entry in educLevel.entries)
                cboEducationLevel.Items.Add(entry.Value);

            SettingGroup educMajor = AppContext.getInstance().settings.getGroup("EducationMajor");
            foreach (KeyValuePair<string, string> entry in educMajor.entries)
                cboMajor.Items.Add(entry.Value);

            
        }

        
        public int participantAge
        {
            get { return (this.txtAge.Text.Trim().Length > 0) ? int.Parse(txtAge.Text.Trim()) : -1; }
        }

        public ParticipantInfo getParticipantInfo()
        {
            if (_participantInfo.registDt == DateTime.MinValue)
                _participantInfo.registDt = DateTime.Now;
            try
            {
                _participantInfo.age = int.Parse(txtAge.Text);
            }
            catch (Exception e) { }
            _participantInfo.gender = this.cboGender.Text;

            _participantInfo.educationLevel = cboEducationLevel.Text;
            if (this.cboEducationLevel.Text.ToLower().StartsWith("other"))
            {
                _participantInfo.educationLevel = this.txtEducLevelOther.Text.Trim();
            }

            _participantInfo.studyArea = this.cboMajor.Text;
            if (this.cboMajor.Text.ToLower().StartsWith("other"))
            {
                _participantInfo.studyArea = this.txtMajorOther.Text.Trim();
            }

            _participantInfo.remark = "";
            return _participantInfo;
        }

        

        protected bool validateFields()
        {
            bool retval = true;
            if (!ControlUtils.checkComboBox(ref this.cboMajor, ref lblMajor))
                retval = false;
            else if (this.cboMajor.Text.ToLower().StartsWith("other"))
            {
                if (!ControlUtils.checkTextBox(ref this.txtMajorOther, ref lblMajorOther))
                    retval = false;
            }

            if (!ControlUtils.checkComboBox(ref this.cboEducationLevel, ref this.lblEducLevel))
                retval = false;
            else if (this.cboEducationLevel.Text.ToLower().StartsWith("other"))
            {
                if (!ControlUtils.checkTextBox(ref this.txtEducLevelOther, ref lblEducLevelOther))
                    retval = false;
            }

            if (!ControlUtils.checkComboBox(ref this.cboGender, ref this.lblGender))
                retval = false;
            if (!ControlUtils.checkTextBox(ref this.txtAge, ref this.lblAge))
                retval = false;
            if (!ControlUtils.checkTextBox(ref this.txtId, ref this.lblId))
                retval = false;

            return retval;
        }

        private void FormUserProfile_Load(object sender, EventArgs e)
        {
            lblEducLevelOther.Visible = false;
            txtEducLevelOther.Visible = false;
            lblMajorOther.Visible = false;
            txtMajorOther.Visible = false;

            //_participantInfo.id = id;
            this.txtId.Text = _participantInfo.id;
            if (_participantInfo.registDt > DateTime.MinValue) // recurrent participant
            {

                this.txtAge.Text = _participantInfo.age.ToString();
                selectComboOption(ref this.cboGender, _participantInfo.gender);
                
                if (selectComboOption(ref this.cboEducationLevel, _participantInfo.educationLevel) == -1)
                {
                    selectComboOption(ref this.cboEducationLevel, "Other");
                    this.txtEducLevelOther.Text = _participantInfo.educationLevel;
                }

                if (selectComboOption(ref this.cboMajor, _participantInfo.studyArea) == -1)
                {
                    selectComboOption(ref this.cboMajor, "Other");
                    this.txtMajorOther.Text = _participantInfo.studyArea;
                }

                lblLastLogin.Text = "Registered: " + _participantInfo.registDt.ToShortDateString();

            }
            else
            {
                lblLastLogin.Text = "(new participant)";
            }


        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (validateFields())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }

            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }


        private void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                // OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            else 
            {
                e.Handled = true;
            }
        }

        public int selectComboOption(ref ComboBox cbo, string text)
        {
            for (int i = 0; i < cbo.Items.Count; i++)
            {
                string item = (string)cbo.Items[i];
                if (item.Equals(text)) {
                    cbo.SelectedIndex = i;
                    return i;
                }
            }
            return -1;
        }

        private void cboEducationLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboEducationLevel.Text.ToLower().StartsWith("other"))
            {
                lblEducLevelOther.Visible = true;
                txtEducLevelOther.Visible = true;
            }
            else
            {
                lblEducLevelOther.Visible = false;
                txtEducLevelOther.Visible = false;
            }
        }

        private void cboMajor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboMajor.Text.ToLower().StartsWith("other"))
            {
                lblMajorOther.Visible = true;
                txtMajorOther.Visible = true;
            }
            else
            {
                lblMajorOther.Visible = false;
                txtMajorOther.Visible = false;
            }
        }


    }
}
